package com.ttai.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.ttai.R;
import com.ttai.dagger.componet.activity.DaggerAddIdAcountComponet;
import com.ttai.dagger.module.activity.AddIdAcountPresenterModule;
import com.ttai.presenter.activity.AddIdAcountPresenter;
import com.ttai.presenter.activity.TaiRegiserPresenter;
import com.ttai.ui.base.BaseActivity;
import com.ttai.ui.other.MessageDialog;
import com.ttai.untils.Constant;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddIdAcount extends BaseActivity {
    @Bind(R.id.sp_list)
    Spinner spList;
    @Bind(R.id.ed_idacountnumber)
    EditText edIdacountnumber;
    @Bind(R.id.ed_idacountpassword)
    EditText edIdacountpassword;
    @Bind(R.id.btn_logincheck)
    Button btnLogincheck;

    @Inject
    AddIdAcountPresenter addIdAcountPresenter;

    TaiRegiserPresenter taiRegiserPresenter;
    @Bind(R.id.btn_regiser)
    Button btnRegiser;
    private String appId;
    public MessageDialog messageDialog;

    public ArrayList<String> idList = new ArrayList<>();
    public ArrayAdapter<String> adapter = null;
    public String nameNumber;
    public String mynumber;
    public String myPassword;
    public String jiamiPassword;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_idacount);
        ButterKnife.bind(this);
        setComponet();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

        idList = addIdAcountPresenter.getListData();
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, idList);
        //设置下拉列表风格
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spList.setAdapter(adapter);
        spList.setVisibility(View.VISIBLE);//设置默认显示


    }

    @Override
    public void next(View view) {

    }

    @Override
    public void pre(View view) {
        finish();
    }

    @Override
    public void onStart() {

        super.onStart();
        adapter.notifyDataSetChanged();
    }

    @Override
    public void setComponet() {
        DaggerAddIdAcountComponet.builder()
                .addIdAcountPresenterModule(new AddIdAcountPresenterModule(this))
                .build()
                .in(this);
    }

//    @OnClick(R.id.btn_logincheck)
//    public void onViewClicked() {
//        nameNumber = spList.getSelectedItem().toString();
//        String appId = addIdAcountPresenter.getAPPId(nameNumber);
//        mynumber = edIdacountnumber.getText().toString();
//        myPassword = edIdacountpassword.getText().toString();
//        try {
//            jiamiPassword = Constant.mD5Code(myPassword);
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        }
//
//
//        String token = Constant.getToken();
//        addIdAcountPresenter.check(appId, token, mynumber, jiamiPassword);
//    }

    public void showMessageDialog(String message) {
        messageDialog = new MessageDialog(AddIdAcount.this);
        messageDialog.setTitle("提示");
        messageDialog.setMessage(message);
        messageDialog.setYesOnclickListener("确定", new MessageDialog.onYesOnclickListener() {
            @Override
            public void onYesClick() {
//                Toast.makeText(AddIdAcount.this,"点击了--确定--按钮",Toast.LENGTH_LONG).show();
                messageDialog.dismiss();
            }
        });
        messageDialog.setNoOnclickListener("取消", new MessageDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
//                Toast.makeText(AddIdAcount.this,"点击了--取消--按钮",Toast.LENGTH_LONG).show();
                messageDialog.dismiss();
            }
        });
        messageDialog.show();
    }

    @OnClick({R.id.btn_logincheck, R.id.btn_regiser})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_logincheck:
                nameNumber = spList.getSelectedItem().toString();
                appId = addIdAcountPresenter.getAPPId(nameNumber);
                mynumber = edIdacountnumber.getText().toString();
                myPassword = edIdacountpassword.getText().toString();
                try {
                    jiamiPassword = Constant.mD5Code(myPassword);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }

                String token = Constant.getToken();
                System.out.println("appId:"+appId+" token:"+token+" mynumber:"+mynumber+" jia:"+jiamiPassword);
                addIdAcountPresenter.check(appId, token, mynumber, jiamiPassword);
                break;
            case R.id.btn_regiser:
                System.out.println("appId:"+appId);
                taiRegiserPresenter = new TaiRegiserPresenter(this);
                taiRegiserPresenter.regiser(Constant.getToken(), appId);
                break;
        }
    }


//    @OnClick({R.id.btn_logincheck, R.id.btn_regiser})
//    public void onViewClicked(View view) {
//        nameNumber = spList.getSelectedItem().toString();
//        appId = addIdAcountPresenter.getAPPId(nameNumber);
//        switch (view.getId()) {
//            case R.id.btn_logincheck:
//                mynumber = edIdacountnumber.getText().toString();
//                myPassword = edIdacountpassword.getText().toString();
//                try {
//                    jiamiPassword = Constant.mD5Code(myPassword);
//                } catch (UnsupportedEncodingException e) {
//                    e.printStackTrace();
//                } catch (NoSuchAlgorithmException e) {
//                    e.printStackTrace();
//                }
//
//                String token = Constant.getToken();
//                addIdAcountPresenter.check(appId, token, mynumber, jiamiPassword);
//                break;
//
//            case R.id.btn_regiser:
//                System.out.println("appId:"+appId);
//                taiRegiserPresenter = new TaiRegiserPresenter(this);
//                taiRegiserPresenter.regiser(Constant.getToken(), appId);
//                break;
//        }
//    }
}
